{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "730758db-ac77-409a-a47b-c82f9859abdd",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn.linear_model import LinearRegression\n",
    "from sklearn.metrics import r2_score, mean_squared_error\n",
    "import warnings\n",
    "\n",
    "warnings.filterwarnings('ignore')\n",
    "\n",
    "data = {\n",
    "    'Price': [43.71, 95.56, 75.88, 63.88, 24.04, 24.04, 15.23, 87.96, 64.1, 73.73, 11.85, 97.29, 84.92, 29.11, 26.36, \n",
    "              26.51, 37.38, 57.23, 48.88, 36.21, 65.07, 22.55, 36.29, 42.97, 51.05, 80.67, 27.97, 56.28, 63.32, 14.18, \n",
    "              64.68, 25.35, 15.85, 95.4, 96.91, 82.76, 37.42, 18.79, 71.58, 49.61, 20.98, 54.57, 13.09, 91.84, 33.29, \n",
    "              69.63, 38.05, 56.81, 59.2, 26.64, 97.26, 79.76, 94.55, 90.53, 63.81, 92.97, 17.96, 27.64, 14.07, 39.28, \n",
    "              44.98, 34.42, 84.59, 42.11, 35.28, 58.84, 22.68, 82.2, 16.71, 98.82, 79.5, 27.88, 10.5, 83.39, 73.62, \n",
    "              75.61, 79.41, 16.66, 42.26, 20.43, 87.68, 66.1, 39.78, 15.72, 37.99, 39.27, 75.66, 67.38, 89.85, 52.5, \n",
    "              20.76, 74.19, 78.47, 60.51, 79.39, 54.44, 57.05, 48.48, 12.29, 19.71],\n",
    "    'Sales': [653, 227, 396, 429, 801, 818, 923, 281, 463, 395, 933, 232, 305, 783, 792, 817, 680, 532, 597, 666, 488, \n",
    "              827, 710, 649, 549, 342, 766, 526, 489, 899, 539, 802, 881, 235, 167, 337, 702, 924, 422, 612, 831, 528, \n",
    "              930, 288, 757, 416, 738, 503, 544, 853, 192, 345, 247, 261, 443, 258, 824, 793, 860, 732, 617, 715, 348, \n",
    "              626, 725, 568, 770, 348, 874, 233, 327, 737, 932, 342, 419, 406, 344, 874, 671, 815, 355, 485, 646, 894, \n",
    "              667, 709, 429, 436, 310, 592, 859, 463, 365, 493, 338, 540, 541, 622, 910, 867]\n",
    "}\n",
    "\n",
    "df = pd.DataFrame(data)\n",
    "\n",
    "print(\"数据基本信息:\")\n",
    "print(f\"数据形状: {df.shape}\")\n",
    "print(\"\\n数据前5行:\")\n",
    "print(df.head())\n",
    "\n",
    "x = df[['Price']]\n",
    "y = df['Sales']\n",
    "\n",
    "model = LinearRegression()\n",
    "model.fit(x, y)\n",
    "\n",
    "print(\"\\n模型参数:\")\n",
    "print(f\"斜率: {model.coef_[0]:.4f}\")\n",
    "print(f\"截距: {model.intercept_:.4f}\")\n",
    "print(f\"回归方程: y = {model.coef_[0]:.4f}x + {model.intercept_:.4f}\")\n",
    "\n",
    "y_pred = model.predict(x)\n",
    "r2 = r2_score(y, y_pred)\n",
    "mse = mean_squared_error(y, y_pred)\n",
    "rmse = np.sqrt(mse)\n",
    "\n",
    "print(f\"R²分数: {r2:.4f}\")\n",
    "print(f\"RMSE: {rmse:.2f}\")\n",
    "\n",
    "plt.figure(figsize=(12, 8))\n",
    "\n",
    "plt.scatter(df['Price'], df['Sales'], alpha=0.7, color='blue', s=50)\n",
    "\n",
    "x_range = pd.DataFrame({'Price': np.linspace(df['Price'].min(), df['Price'].max(), 100)})\n",
    "y_range = model.predict(x_range)\n",
    "plt.plot(x_range['Price'], y_range, color='red', linewidth=2)\n",
    "\n",
    "plt.xlabel('Price')\n",
    "plt.ylabel('Sales')\n",
    "plt.title('Price vs Sales - Linear Regression')\n",
    "\n",
    "equation_text = f'y = {model.coef_[0]:.4f}x + {model.intercept_:.4f}\\nR² = {r2:.4f}\\nRMSE = {rmse:.2f}'\n",
    "plt.text(0.65, 0.85, equation_text, transform=plt.gca().transAxes, fontsize=12)\n",
    "\n",
    "plt.grid(True, alpha=0.3)\n",
    "plt.tight_layout()\n",
    "plt.show()\n",
    "\n",
    "sample_prices = [10, 30, 50, 70, 90]\n",
    "print(\"\\n预测示例:\")\n",
    "for price in sample_prices:\n",
    "    prediction = model.predict(pd.DataFrame({'Price': [price]}))[0]\n",
    "    print(f\"价格 = {price:.1f}, 预测销售额 = {prediction:.1f}\")\n",
    "\n",
    "correlation = df['Price'].corr(df['Sales'])\n",
    "print(f\"\\n相关系数: {correlation:.4f}\")\n",
    "\n",
    "price_change = 10\n",
    "sales_change = model.coef_[0] * price_change\n",
    "print(f\"\\n价格弹性:\")\n",
    "print(f\"价格每增加{price_change}个单位，销售额变化: {sales_change:.1f}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
